﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Gmantis.Controls.Toolbar
{
    internal static class Utils
    {
        public static Geometry ClipBorder(Size sz, Border bdr)
        {
            CornerRadius cornerRadius = bdr.CornerRadius;
            Thickness borderThickness = bdr.BorderThickness;
            PathGeometry geometry = new PathGeometry();
            PathFigure figure = new PathFigure();
            double topLeft = cornerRadius.TopLeft;
            double x = 0.0;
            double y = 0.0;
            figure.StartPoint = new Point(x, topLeft + y);
            if (cornerRadius.TopLeft > 0.0)
            {
                ArcSegment segment = new ArcSegment
                {
                    Point = new Point(topLeft + x, y),
                    Size = new Size(topLeft, topLeft),
                    SweepDirection = SweepDirection.Clockwise
                };
                figure.Segments.Add(segment);
            }
            double topRight = cornerRadius.TopRight;
            double num5 = borderThickness.Right + borderThickness.Left;
            double num6 = 0.0;
            LineSegment segment2 = new LineSegment
            {
                Point = new Point((sz.Width - topRight) - num5, num6)
            };
            figure.Segments.Add(segment2);
            if (cornerRadius.TopRight > 0.0)
            {
                ArcSegment segment3 = new ArcSegment
                {
                    Point = new Point(sz.Width - num5, topRight + num6),
                    Size = new Size(topRight, topRight),
                    SweepDirection = SweepDirection.Clockwise
                };
                figure.Segments.Add(segment3);
            }
            double bottomRight = cornerRadius.BottomRight;
            double num8 = borderThickness.Right + borderThickness.Left;
            double num9 = borderThickness.Bottom + borderThickness.Top;
            LineSegment segment4 = new LineSegment
            {
                Point = new Point(sz.Width - num8, (sz.Height - bottomRight) - num9)
            };
            figure.Segments.Add(segment4);
            if (cornerRadius.BottomRight > 0.0)
            {
                ArcSegment segment5 = new ArcSegment
                {
                    Point = new Point((sz.Width - bottomRight) - num8, sz.Height - num9),
                    Size = new Size(bottomRight, bottomRight),
                    SweepDirection = SweepDirection.Clockwise
                };
                figure.Segments.Add(segment5);
            }
            double bottomLeft = cornerRadius.BottomLeft;
            double num11 = 0.0;
            double num12 = borderThickness.Bottom + borderThickness.Top;
            LineSegment segment6 = new LineSegment
            {
                Point = new Point(bottomLeft - num11, sz.Height - num12)
            };
            figure.Segments.Add(segment6);
            if (cornerRadius.BottomLeft > 0.0)
            {
                ArcSegment segment7 = new ArcSegment
                {
                    Point = new Point(num11, (sz.Height - bottomLeft) - num12),
                    Size = new Size(bottomLeft, bottomLeft),
                    SweepDirection = SweepDirection.Clockwise
                };
                figure.Segments.Add(segment7);
            }
            figure.IsClosed = true;
            geometry.Figures.Add(figure);
            return geometry;
        }

        public static bool IsEmpty(this CornerRadius cr)
        {
            return ((((cr.BottomLeft == 0.0) && (cr.BottomRight == 0.0)) && (cr.TopLeft == 0.0)) && (cr.TopRight == 0.0));
        }
    }
}
